/**
 * com.chaeron.GPS - An API for access to GPS devices.
 *
 *
 * Copyright (c) 2001, 2002 Chaeron Corporation,
 * All rights reserved.
 * 
 * Chaeron grants you a non-exclusive license to use, modify and re-distribute
 * this program under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version, provided that this copyright notice and 
 * license appear on all copies of the software.
 * 
 * Software is provided "AS IS," without a warranty of any kind. ALL EXPRESS OR
 * IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
 * HEREBY EXCLUDED. THE ENTIRE RISK ARISING OUT OF USING THE SOFTWARE IS ASSUMED
 * BY THE LICENSEE. See the GNU General Public License for more details.
 *
 * CHAERON AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR DISTRIBUTING SOFTWARE.
 * IN NO EVENT WILL CHAERON OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE,
 * PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL
 * OR PUNITIVE DAMAGES, HOWEVER CAUSED AN REGARDLESS OF THE THEORY OF LIABILITY,
 * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF CHAERON HAS
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 * @version     1.02
 * @author      Andrzej Jan Taramina, Chaeron Corporation
 *              andrzej@chaeron.com
 */

package com.chaeron.GPS.NMEA;

//***************************************************************************
//*
//*     Imports
//*
//***************************************************************************/

import com.chaeron.GPS.*;
import com.chaeron.GPS.event.*;
import com.chaeron.GPS.exception.*;

//***************************************************************************
//*
//*     RTESentence Class definition
//*
//***************************************************************************/

/**
 * The RTESentence class represents NMEA RTE - Route Navigation
 *
 */

public class RTESentence extends NMEASentence implements GPSNavRoute
{
	//***************************************************************************
	//*
	//*     Constant definitions
	//*
	//***************************************************************************/
	
	private final static int		RTE_NUM_FIELDS				= 3;
	
	
	//***************************************************************************
	//*
	//*     Attributes
	//*
	//***************************************************************************/
	
	private int			waypoints					= 0;
	
	private short		msgCount					= 0;
	private short		msgNumber					= 0;
	private char		mode						= 0;
	
	
	//***************************************************************************
	//*
	//*     Constructors
	//*
	//***************************************************************************/
	
	/**
	 * Create a RTESentence from the given packet data
	 *
	 * @param	tid			talker ID
	 * @param	sid			Sentence ID
	 * @param	sentenceStr	sentence string buffer
	 */
	
	RTESentence( int tid, int sid, String sentenceStr )
	{  
		super( tid, sid, sentenceStr );
		
		setFieldCount( RTE_NUM_FIELDS );
	}
	
	
	//***************************************************************************
	//*
	//*     Methods
	//*
	//***************************************************************************/
	
	/**
	 * Decode the data fields for a RTE sentence
	 *
	 * @return		This method will return a standard error code or GPS.ERROR_OK
	 * @throws GPSPacketException
	 * @throws GPSFunctionUnavailableException
	 */
	
	public void parseData() throws GPSPacketException, GPSFunctionUnavailableException
	{  
		int		i;
		
		waypoints = getFieldCount() - 3;
		
		if( getFieldCount() > RTE_NUM_FIELDS && waypoints > 0 ) {
			msgCount			= getNMEAShort( getField( 0 ) );		// Total Message Count
			msgNumber			= getNMEAShort( getField( 1 ) );		// Message Number
			mode				= getNMEAChar( getField( 2 ) );			// mode (c = complete, w = working)
		} else {
			throw( new GPSPacketException( GPSPacketException.EXCEPTION_NUMFIELDS_INVALID ) );
		}
	}
	
	
	//***************************************************************************
	//*
	//*     Sentence Processing Methods
	//*
	//***************************************************************************/
	
	/**
	 * Process the sentence
	 *
	 * @param	gps			the GPS object that requested the RTE sentence to be processed
	 */
	
	void process( GPS gps )
	{
		gps.postGPSEvent( new GPSEvent( GPSEvent.GPS_EVENT_NAVIGATION_ROUTE, this ) );
	}
	
	
	/**
	 * Get events generated.
	 *
	 * @return		return a valid event type(s) that will be generated by this sentence subclass
	 */
	
	static int getEventsGenerated()
	{  
		return( GPSEvent.GPS_EVENT_NAVIGATION_ROUTE | GPSEvent.GPS_EVENT_NMEA_SENTENCE );
	}
	
	
	/**
	 * Does the object contain valid data?
	 *
	 * @return		Does the object contain valid data?
	 */
	
	public boolean isDataValid()
	{  
		return( waypoints > 2 || ( waypoints == 1 && getField( 3 ).length() > 0 ) );
	}
	
	
	//***************************************************************************
	//*
	//*    Accessor methods
	//*
	//***************************************************************************/
	
	/**
	 * Get the Message Count
	 *
	 * @return		Message Count
	 */
	
	public short getMessageCount()
	{  
		return( msgCount );
	}
	
	
	/**
	 * Get the Message Number
	 *
	 * @return		Message Number
	 */
	
	public short getMessageNumber()
	{  
		return( msgNumber );
	}
	
	
	/**
	 * Get the Mode ( c = complete route, all waypoints,
	 *                w = working route, waypoints just left, waypoint heading towards
	 *                followed by rest of waypoints )
	 *
	 * @return		Mode
	 */
	
	public char getMode()
	{  
		return( mode );
	}
	
	
	/**
	 * Get number of waypoints in route
	 *
	 * @return	 number of waypoints in route
	 */
	
	public int getWaypointCount()
	{  
		return( waypoints );
	}
	
	
	/**
	 * Get the specified satellite number
	 *
	 * @param	index	index of satellite
	 * @return			satellite number
	 */
	
	public String getWaypoint( int index )
	{  
		String		value = "";
		
		if( index >= 0 && index < waypoints ) {
			value = (String)getField( index + RTE_NUM_FIELDS );
		}
		
		return( value );
	}
	
	/**
	 * Get extra field count.  Always returns zero for this Sentence type
	 *
	 * @return		zero
	 */
	
	public int getExtraFieldCount()
	{  
		return( 0 );
	}
	
}
